//
//  NSString+NSDate.m
//  Beautiful
//
//  Created by admin on 2017/6/8.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "NSString+NSDate.h"

@implementation NSString (NSDate)

+(NSString *)compareTimeWithBossTime:(NSString *)str
{
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    NSLog(@"%ld----%ld",timeDate.hour,timeDate.minute);
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    //timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if(timeInterval/60/60 <24){
        result = [NSString stringWithFormat:@"今天 %02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    
    else if(timeInterval/60/60 <48){
        result = [NSString stringWithFormat:@"昨天 %02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    
    else if(timeInterval/60/60 <72){
        result = [NSString stringWithFormat:@"前天 %02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    else if(timeInterval/60/60/24<365){
        temp = temp/12;
        result = [NSString stringWithFormat:@"%02ld-%02ld %02ld:%02ld",timeDate.month,timeDate.day,timeDate.hour,timeDate.minute];
    }else{
        result = [NSString stringWithFormat:@"%02ld-%02ld-%02ld %02ld:%02ld",timeDate.year,timeDate.month,timeDate.day,timeDate.hour,timeDate.minute];
    }
    
    
    
    
    
    
    
    
    
    return result;
}


+ (NSString *)compareCurrentTime:(NSString *)str
{

    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    NSLog(@"%ld----%ld",timeDate.hour,timeDate.minute);
    
    //得到与当前时间差
    NSTimeInterval  timeInterval =[timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
   // timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if(timeInterval/60<60){
        result = [NSString stringWithFormat:@"%ld分钟前",(long)timeInterval/60];
    }
    
    else if(timeInterval/60/60 <24){
        result = [NSString stringWithFormat:@"%ld小时前",(long)timeInterval/60/60];
    }
    
    else if(timeInterval/60/60 <48){
        result = [NSString stringWithFormat:@"昨天%02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    
    else if(timeInterval/60/60 <72){
        result = [NSString stringWithFormat:@"前天%02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    else if(timeInterval/60/60/24<365){
        temp = temp/12;
        result = [NSString stringWithFormat:@"%02ld-%02ld %02ld:%02ld",timeDate.month,timeDate.day,timeDate.hour,timeDate.minute];
    }else{
        result = [NSString stringWithFormat:@"%02ld-%02ld-%02ld %02ld:%02ld",timeDate.year,timeDate.month,timeDate.day,timeDate.hour,timeDate.minute];
    }
    
    return  result;
}

+(NSString *)compareCurrentTime:(NSString *)str AndNowTime:(NSString *)nowTime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    nowTime=[nowTime substringToIndex:19];
    nowTime=[nowTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    
    
    NSDate *nowDate= [dateFormatter dateFromString:nowTime];
    
    
    
    NSTimeInterval  timeInterval = [nowDate timeIntervalSinceDate:timeDate];
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if(timeInterval/60<60){
        result = [NSString stringWithFormat:@"%ld分钟前",(long)timeInterval/60];
    }
    
    else if(timeInterval/60/60 <24){
        result = [NSString stringWithFormat:@"%ld小时前",(long)timeInterval/60/60];
    }
    
    else if(timeInterval/60/60 <48){
        result = [NSString stringWithFormat:@"昨天%02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    
    else if(timeInterval/60/60 <72){
        result = [NSString stringWithFormat:@"前天%02ld:%02ld",timeDate.hour,timeDate.minute];
    }
    else if(timeInterval/60/60/24<365){
        temp = temp/12;
        result = [NSString stringWithFormat:@"%02ld-%02ld %02ld:%02ld",timeDate.month,timeDate.day,timeDate.hour,timeDate.minute];
    }else{
        result = [NSString stringWithFormat:@"%02ld-%02ld-%02ld %02ld:%02ld",timeDate.year,timeDate.month,timeDate.day,timeDate.hour,timeDate.minute];
    }
     return  result;
}









+ (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *startD =[date dateFromString:startTime];
    NSDate *endD = [date dateFromString:endTime];
    BOOL state = [startD isYesterdayAndNowDate:startD];
    
    

    NSTimeInterval start = [startD timeIntervalSince1970]*1;
    NSTimeInterval end = [endD timeIntervalSince1970]*1;
    NSTimeInterval value = end - start;
    int second = (int)value %60;//秒
    int minute = (int)value /60%60;
    int house = (int)value / (24 * 3600)%3600;
    int day = (int)value / (24 * 3600);
    NSString *str;
    if (day != 0) {
        str = [NSString stringWithFormat:@"耗时%d天%d小时%d分%d秒",day,house,minute,second];
    }else if (day==0 && house != 0) {
        str = [NSString stringWithFormat:@"耗时%d小时%d分%d秒",house,minute,second];
    }else if (day== 0 && house== 0 && minute!=0) {
        str = [NSString stringWithFormat:@"耗时%d分%d秒",minute,second];
    }else{
        str = [NSString stringWithFormat:@"耗时%d秒",second];
    }
    return str;
}







@end
